package com.lagou.servlet;

import com.lagou.bean.Classe;
import com.lagou.service.ClassService;
import com.lagou.util.JSONUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ClassServlet", urlPatterns = "/classes")
public class ClassServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求中的学生查询条件信息
        String action = request.getParameter("action");
        System.out.println("操作类型为：" + action);

        //创建ClassService类型的对象去实现数据的校验功能
        ClassService classService = new ClassService();

        //查询学生信息
        if("listAll".equals(action)){
            List<Classe> list = classService.claQurService();
            System.out.println(list);
            if (null != list) {
                // 返回JSON数据格式
                JSONArray jsonArr = JSONArray.fromObject(list);
                JSONObject jsonObjOut = new JSONObject();
                jsonObjOut.put("list", jsonArr);
                JSONUtil.returnJSON(request, response, jsonObjOut);
            }
        }
        //添加学生信息
        else if("add".equals(action)){
            // 获取JSON数据
            JSONObject jsonObjIn = JSONUtil.getJSON(request, response);

            System.out.println(jsonObjIn.toString());
            Classe cla = new Classe();
            cla.setCid(jsonObjIn.getString("cid"));
            cla.setGrade(Integer.valueOf(jsonObjIn.getString("grade")));
            cla.setHeadMaster(jsonObjIn.getString("headMaster"));
            cla.setSlogan(jsonObjIn.getString("slogan"));
            cla.setNum(Integer.valueOf(jsonObjIn.getString("num")));

            boolean isSuccess = classService.addStd(cla);
            // 返回JSON数据格式
            JSONObject jsonObjOut = new JSONObject();
            jsonObjOut.put("isSuccess", isSuccess);
            JSONUtil.returnJSON(request, response, jsonObjOut);

        }

        //更新学生信息
        else if("update".equals(action)){
            // 获取JSON数据
            JSONObject jsonObjIn = JSONUtil.getJSON(request, response);

            System.out.println(jsonObjIn.toString());
            Classe cla = new Classe();
            cla.setCid(jsonObjIn.getString("cid"));
            cla.setGrade(jsonObjIn.getInt("grade"));
            cla.setHeadMaster(jsonObjIn.getString("headMaster"));
            cla.setSlogan(jsonObjIn.getString("slogan"));
            cla.setNum(jsonObjIn.getInt("num"));

            boolean isSuccess = classService.updateStd(cla);
            // 返回JSON数据格式
            JSONObject jsonObjOut = new JSONObject();
            jsonObjOut.put("isSuccess", isSuccess);
            JSONUtil.returnJSON(request, response, jsonObjOut);
        }

        //删除学生信息
        else if("delete".equals(action)) {
            // 获取JSON数据
            JSONObject jsonObjIn = JSONUtil.getJSON(request, response);

            Classe cla = new Classe();
            cla.setCid(jsonObjIn.getString("cid"));

            //查询是否存在学生
            boolean isExsist = classService.isExsistStd(cla.getCid());
            boolean isSuccess = false;
            if(!isExsist){
                isSuccess = classService.delCla(cla);
            }
            // 返回JSON数据格式
            JSONObject jsonObjOut = new JSONObject();
            jsonObjOut.put("isSuccess", isSuccess);
            JSONUtil.returnJSON(request, response, jsonObjOut);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
